﻿namespace Web.Control.WWW.Answers
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    using System.IO;
    using Lx.Web.Common.ControlsUI;
    using Lx.Web.Common.UI;
    using Model.WWW_WWW.Answers;
    using BLF.WWW_WWW.Answer;
    using System.Collections.Generic;
    using BLF;

    public class Default : BaseControl
    {
        /// <summary>
        /// 显示的类型（1为Popular，2为Need Answers，3为Latest）
        /// </summary>
        protected int Dtype=1;
       /// <summary>
       /// 页面
       /// </summary>
        protected int pageIndex = 1;
        /// <summary>
        /// 目标语言
        /// </summary>
        protected string Learn = "";
        /// <summary>
        /// 使用语言
        /// </summary>
        protected string BaseL = "";
        protected string MyLL = "";
        protected string MyBL = "";
        protected string memberno = "";
        protected Repeater repQuest;
        protected Repeater repTags;
        protected Repeater rptLL;
        /// <summary>
        /// 数据数量
        /// </summary>
        protected int RecordCount = 0;
        protected string sortField = "views";
        protected List<AnswersTagModel> anList;
        protected List<QuestionModel> quList;
        protected override void OnLoad(EventArgs e)
        {

            if (Request.Cookies["italkiLearn"] == null)
            {
                HttpCookie llCookie = new HttpCookie("italkiLearn", "");                
                Response.Cookies.Add(llCookie);                
            }
            if(Request.Cookies["italkiSpeak"] == null)
            {
                HttpCookie blCookie = new HttpCookie("italkiSpeak", "");
                Response.Cookies.Add(blCookie);
            }
            if (!string.IsNullOrEmpty(Request["dtype"]))
            {
                if (Request["dtype"].ToString() == "2")
                {
                    if ( HttpContext.Current.User.Identity.Name== "")
                    {
                        string url = Page.Request.Url.AbsoluteUri.Split('?')[0];
                        Page.Response.Redirect(LoginHost + "/User/login.htm?ReturnUrl=" + url);
                        return;
                    }
                }
                Dtype = int.Parse(Request["dtype"].ToString());
                if (Dtype == 1)
                {
                    sortField = "views";
                }
                else
                {
                    sortField = "PostDate";
                }
            }
            if (!string.IsNullOrEmpty(Request["page"]))
            {
                if (Request["page"] == "L")
                {
                    pageIndex = 999;
                }
                else
                {
                    int.TryParse(Request["page"], out pageIndex);
                }
            }
            if (!string.IsNullOrEmpty(Request["ll"]) && Request["ll"] != "")
            {
                Learn = Request["ll"];
                if (Dtype != 2)
                {
                    HttpCookie llCookie = new HttpCookie("italkiLearn", Learn);
                    Response.Cookies.Add(llCookie);
                }                
            }
            else
            {
                Learn = Request.Cookies["italkiLearn"].Value;
            }
            if (!string.IsNullOrEmpty(Request["bl"]) && Request["bl"] != "")
            {
                BaseL = Request["bl"];
                if (Dtype != 2)
                {
                    HttpCookie blCookie = new HttpCookie("italkiSpeak", BaseL);
                    Response.Cookies.Add(blCookie);
                }                
            }
            else 
            {
                BaseL = Request.Cookies["italkiSpeak"].Value;
            }
            if (HttpContext.Current.User.Identity.Name != "" && string.IsNullOrEmpty(Request["ll"]) && string.IsNullOrEmpty(Request["bl"]) && Request["dtype"] !="3" && Request["dtype"] != "1")
            {
                memberno = HttpContext.Current.User.Identity.Name;
                quList = Answers.GetQuestion(memberno, pageIndex, 15, out RecordCount, out anList, out BaseL, out Learn);
                if ((Request.Cookies["italkiLearn"] == null && Request.Cookies["italkiSpeak"] == null) || (Request.Cookies["italkiLearn"].Value == "" && Request.Cookies["italkiSpeak"].Value == ""))
                {
                    HttpCookie llCookie = new HttpCookie("italkiLearn", BaseL);
                    HttpCookie blCookie = new HttpCookie("italkiSpeak", Learn);
                    Response.Cookies.Add(llCookie);
                    Response.Cookies.Add(blCookie);
                }
                BaseL = "";
               Dtype = 2;
            }
            else
            {
                
                quList = Answers.GetQuestion( pageIndex, 15, Learn, BaseL, sortField,Dtype, out RecordCount, out anList);
                int pCount = (int)Math.Ceiling(((decimal)RecordCount) / ((decimal)15));
                if (pageIndex > pCount) pageIndex = pCount;
            }
            
            if (repQuest != null)
            {
                repQuest.DataSource = quList;
                repQuest.DataBind();
            }
            if (repTags != null)
            {
                repTags.DataSource = anList;
                repTags.DataBind();
            }
            if (rptLL != null)
            {                
                List<LanguageObj> lanObjList = new List<LanguageObj>();
                lanObjList.AddRange(BLF.StaticData.GetPopLanguageList);
                rptLL.DataSource = lanObjList;
                rptLL.DataBind();
            }
            Lx.Web.Common.UI.TDTPageBase thisPage = (Lx.Web.Common.UI.TDTPageBase)this.Page;
            //if (thisPage.metaTitle != null)
            //{
            //    if(Dtype==1)
            //    {
            //    thisPage.metaTitle.Text =GetText("AS500");
            //    thisPage.metaKey.Text=String.Format(thisPage.metaKey.Text,GetText("AS501"));
            //    thisPage.metaDes.Text =String.Format(thisPage.metaDes.Text,GetText("AS502"));
            //    }
            //    else if(Dtype==3)
            //    {
            //        thisPage.metaTitle.Text = String.Format(thisPage.metaTitle.Text, GetText("AS503"));
            //        thisPage.metaKey.Text = String.Format(thisPage.metaKey.Text, GetText("AS504"));
            //        thisPage.metaDes.Text = String.Format(thisPage.metaDes.Text, GetText("AS505"));
            //    }
            //}
        }

    }
}